<html> 
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
        <body id="body">
        <div id="header">  
        </div>

        <div id="menu">
            <ul id="a">
                <li><a href="index.php">Trang Chu</a></li>
                <li><a href="#">Gioi Thieu</a></li>
                <li><a href="#">Dien Dan</a></li>
                <li><a href="#">Lien He</a></li>
                  <li>   <?php if(isset($_SESSION["id"])): ?>
                 <div id="logout"><a href="logout.php">LOGOUT</a></div>
                    <?php                    endif; ?></li>
                   
            </ul>
           
        </div>
             <div id="page_body">  
                 <div class="logo"></div>
<?php 
require_once '../model/work.php';
$work= new works();
$works=$work->select_edit(null, $_GET['id']);
?>
<form method="post" action="edit.php?id=<?php echo $_GET["id"]?>"enctype="multipart/form-data"> 
                <label for="file">Name:</label>
                <input type="text" name="name" value="<?php echo $works["name"]?>"><br>
                
                <div style="float: left; width: 10%;"><label for="altText">Description</label></div>
                        <div style ="width: 60%;"><textarea name="des">
                            <?php echo $works["description"] ?>"</textarea></div>
                <div style="clear: both"></div>
                <label for="file">Filename:</label> 
                <div style="clear: both"></div>
                <input type="file" name="file" id="file"><br>
                <input type="submit" name="submit" value="Submit">  
            </form>
            
 <?php

if (isset($_POST['submit'])) {
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/jpg")
            || ($_FILES["file"]["type"] == "image/pjpeg")
            || ($_FILES["file"]["type"] == "image/x-png")
            || ($_FILES["file"]["type"] == "image/png"))
            
            && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        } else {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

            if (file_exists("../image/" . $_FILES["file"]["name"])) {
                echo $_FILES["file"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"], "../image/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "../image/" . $_FILES["file"]["name"];
            }
            $hinh = "../image/".$_FILES["file"]["name"];
            echo "<img src='$hinh' width = '200px'<br> /><br>";
        }
    } 
     else {
                        $array = array(
                                    "id" => $_GET["id"],
                                    "name" => $_POST["name"],
                                    "des" => $_POST["des"],
                                );
                                $works->update_noimg($array);
}}
?>
</body>
</html>

